/****************************************************
    ScriptName:        boss2.cs
    Author:            摸鱼程序男
    CreatTime:    2020/12/24 16:51:15
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using DG.Tweening;
using System.Linq;
using BTAI;
using ZYKTool;
using ZYKTool.Timer;
using LongZhiJie;

namespace HQL
{
	public class Boss2 : BossBase
    {
        public GameObject _毒针;
        public GameObject _毒块;
        protected int range_dash;
        protected int timeRange;
        protected float _伤害次数=0;
        protected int _damage;
        protected float _speed;


        public override void Init()
        {

            base.Init();
            _aiRoot.OpenBranch(
                BT.If(CanAttack).OpenBranch(
                    BT.Call(Attack)
                )
            /*
           BT.While(die1).OpenBranch(
                 BT.Wait(1),
                 BT.Call(换阶段)
                  )
            */
            );
            _SR = transform.GetOrAddComponent<SpriteRenderer>();
            // ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(换阶段, 1, 1, PETimeUnit.Second);
            _AttackTimer = 3;
            _MyColor = _SR.color;
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss2].Blood;
            BossName = BossName.Boss2;
            _damage = 1;
            _speed = 50;
    }
        private void Shoot旋转(int damage, float speed,GameObject bulletPrefab)
        {
            if (_已经死亡) return;
            PlaySound();
           // Debug.LogError("旋转");
            //todo:longzhijie
            float sumtamp = 12;
            int j = 0;
            //Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
            for (int i = 0; i < sumtamp; i++, j++)//向四周发射5根毒针
            {
                // Debug.LogError(transform.rotation);
              GameObject go = GameObject.Instantiate(bulletPrefab, transform.position, this.transform.rotation) as GameObject;

               // bullet.transform.LookAt(bullet.transform.position + bullet.transform.right);

                BossBullet bossbullet = go.GetOrAddComponent<BossBullet>();
                bossbullet.Damage = damage;
                bossbullet.speed = speed;
             //   go.transform.LookAt(go.transform.position + go.transform.right);
               
                if (UnityEngine.Random.Range(0, 15) < 5)
                {
                   go.transform.Rotate(new Vector3(0, 0, j * 30));
                }
                else if(UnityEngine.Random.Range(0, 15) >= 5&& UnityEngine.Random.Range(0, 15) < 10)
                {

                    go.transform.Rotate(new Vector3(0, 0, j * 20));
                }else
                {

                    go.transform.Rotate(new Vector3(0, 0, j * 20));
                }
                //go.transform.position += go.transform.forward * 0.3f;
                bossbullet.dir = go.transform.up;
                //go.GetComponent<Rigidbody>().velocity = go.transform.forward * speed;
            }
            transform.Rotate(new Vector3(0,0, 45));
        }
        protected void Shoot连续(int damage, float speed)
        {
            if (_已经死亡) return;
            PlaySound();
            // Debug.LogError("连续");
            Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
            GameObject bullet = Instantiate(_毒针, transform.position, Quaternion.Euler(dir));
            BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
            bossbullet.Damage = damage;
            bossbullet.speed = speed;
            bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
            //Debug.LogError(bossbullet.dir);
        }

        #region 攻击类

        public override void 换阶段()
        {
            base.换阶段();
            //Debug.LogError("二级模式切换阶段！");
            _AttackTimer = 2f;
        }
        private void Attack1()
        {
            if (_已经死亡) return;
            //环绕射击
            _SR.color = Color.blue;
            if (UnityEngine.Random.Range(0, 10) >= 7)
            {
                Shoot旋转(1,80,_毒针);

                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {
                        if (_已经死亡) return;
                        //   Debug.LogError("dash");
                        Shoot旋转(1,80, _毒针);
                        _AttackOver = true;
                        _SR.color = Color.red;
                    },
                     1f, 1, PETimeUnit.Second);//1s后继续发射
            }
            else
            {
  
                //发射子弹
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {

                        if (_已经死亡) return;
                        Shoot连续(1,100);

                    },
                    0.5f, 5, PETimeUnit.Second);


                //发射子弹
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {
                        if (_已经死亡) return;
                        _AttackOver = true;
                        _SR.color = Color.red;

                    },
                    3f, 5, PETimeUnit.Second);
                    }


        }


        private void Attack2()
        {
            if (_已经死亡) return;
            //环绕射击
            _SR.color = Color.blue;
            if (UnityEngine.Random.Range(0, 10) >= 4)
            {
                Shoot旋转(1, 80, _毒块);

                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {
                        if (_已经死亡) return;
                        //   Debug.LogError("dash");
                        Shoot旋转(1, 80, _毒块);
                        Shoot旋转(1, 80, _毒块);
                        _AttackOver = true;
                        _SR.color = _MyColor;
                    },
                     0.7f, 1, PETimeUnit.Second);//1s后继续发射
            }
            else
            {

                //发射子弹
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {

                        if (_已经死亡) return;
                        Shoot连续(1, 150);

                    },
                    0.5f, 8, PETimeUnit.Second);//三到五秒才射击


                //发射子弹
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {
                        if (_已经死亡) return;
                        _AttackOver = true;
                        _SR.color =_MyColor;

                    },
                    4f, 5, PETimeUnit.Second);//三到五秒才射击
            }




        }
        protected override void Attack()
        {
            if (_已经死亡) return;
            //Debug.LogError("attack");
            switch (当前阶段)
            {
                case 1:
                    Attack1();
                    break;
                case 2:
                    Attack2();
                    //TODO:贺启亮 换阶段之后如果需要改变参数写这里
                    break;
            }
        }


      
        #endregion
        public override void BossHunt(float damage)
        {
            _伤害次数++;
            if (_伤害次数 >= 5) 
            {
                Shoot2(_damage, _speed);
                _伤害次数 = 0;
            }
            TapTapStaticValue.TapTapModelManger.Damage(BossName.Boss2, damage);
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss2].Blood;
            if (TapTapStaticValue.BossAttributes[BossName.Boss2].Blood <= 0)
            {
                PlaySound(2);
                Die(BossName.Boss2);
                TapTapStaticValue.TapTap升级系统.TapTapStartUpdate();
            }
            else if (TapTapStaticValue.BossAttributes[BossName.Boss2].Blood <=200&& 当前阶段<2)
            {
                换阶段();
            }
        }
        protected void Shoot2(int damage, float speed)
        {
            ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                       (() =>
                       {

                           if (_已经死亡) return;
                           
                           int j = 0;
                           //Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                           for (int i = 0; i < 5; i++, j++)//散弹枪
                           {
                               // Debug.LogError(transform.rotation);

                               // Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
                               GameObject bullet = Instantiate(_毒块, transform.position, Quaternion.identity);
                               BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
                               bossbullet.Damage = damage;
                               bossbullet.speed = speed + 10f;
                               bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;





                           }

                       },
                       0.2f, 3, PETimeUnit.Second);//三到五秒才射击
           

        }

    }
}